<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<script type="text/javascript" src="/js/jquery-3.5.1.js"></script>
<body>
<div style="width: 800px;margin:auto">
    <form method="post" action="/page" style="display: inline">
        <input type="text" name="name">
        <input type="submit" value="查询"/>
    </form>

    <button onclick="add()">新增</button>
    <button onclick="del()">删除</button>
    <table border="1px">
        <tr>
            <th>选择</th>
            <th>编号</th>
            <th>书名</th>
            <th>价格</th>
            <th>类目</th>
            <th>库存</th>
            <th>图片</th>
            <th>说明</th>
            <th>作作者</th>
            <th>已售</th>
            <th>操作</th>
        </tr>

        <tr th:each="book:${page.list}">
            <td>
                <input type="checkbox" th:value="${book.getId()}">
            </td>
            <td th:text="${book.getId()}"></td>
            <td th:text="${book.getName()}"></td>
            <td th:text="${book.getPrice()}"></td>
            <td th:text="${book.getCategory()}"></td>
            <td th:text="${book.getPnum()}"></td>
            <td th:text="${book.getImgurl()}"></td>
            <td th:text="${book.getDescription()}"></td>
            <td th:text="${book.getAuthor()}"></td>
            <td th:text="${book.getSales()}"></td>
            <td>
                <a th:href="@{/update(id=${book.getId()})}">
                    修改
                </a>
            </td>
        </tr>
    </table>

    总页数：<span th:text="${page.pages}"></span>
    总条数：<span th:text="${page.total}"></span>
    当前页的记录数：<span th:text="${page.pageSize}"></span>
    当前页：<span th:text="${page.pageNum}"></span>
    &nbsp;
    &nbsp;
    &nbsp;
    <a th:if="${page.pageNum != 1}" th:href="@{/page(pageNum=${page.pageNum - 1})}">上一页</a>
    <span th:each="pageNum:${#numbers.sequence(1,page.pages)}">
        <a th:href="@{/page(pageNum=${pageNum})}" th:text="${pageNum}"></a>
    </span>
    <a th:if="${page.pageNum != page.pages}" th:href="@{/page(pageNum=${page.pageNum + 1})}" href="">下一页</a>


</div>
</body>

<script>
    function add() {
        window.location.href = "/add";
    }

    function del() {
        if (confirm("确认删除吗?")) {
            var ids = "";
            $("input[type=checkbox]:checked").each(function (i, e) {
                var id = $(e).val();
                ids += id + ",";
            })

            if(ids == ""){
                alert("请选择需要删除的图书信息");
            }else{
                ids = ids.substring(0,ids.length - 1);
                location.href='/del?ids='+ids;
            }
        }
    }
</script>
</html>